﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using Fusion;

namespace Fusion.Interface
{
	public class LabelList : Control
	{
		public string title;
		public Color titleColor = Color.White;
		public SpriteFont font;
		Vector2 titleOffset;

		public List<Label> labels = new List<Label>();
		Vector2 labelSize;
		float labelSpacing;
		int displaySize;

		public int selectedIndex;

		public IntDelegate ItemClicked;

		public LabelList( string Title, Vector2 topLeft, Vector2 LabelSize, float spacing, int displayAmount )
		{
			title = Title;
			font = Engine.Fonts.Get( "Retro" );

			position = topLeft - ( LabelSize / 2 );
			if( title != "" )
			{
				position.Y += LabelSize.Y + spacing;
				titleOffset.Y = -( LabelSize.Y + spacing );
				titleOffset.X = -( LabelSize.X / 2 ) + 8;
			}

			labelSize = LabelSize;
			labelSpacing = spacing;
			displaySize = displayAmount;

			drawBorder = false;
		}

		public Label Add( string text )
		{
			Label label = new Label( new Vector2( 8, 0 ), labelSize, text, true, null );
			label.container = this;
			labels.Add( label );
			return label;
		}

		/// <summary> Sets the text of a particular label. If it does not exist, it will be created </summary>
		/// <param name="index">The intended index of the label</param>
		/// <param name="text">The text to use</param>
		/// <returns>The label, found or created</returns>
		public Label Set( int index, string text )
		{
			Label label = null;

			if( index >= labels.Count )
				label = Add( text );
			else
				labels[index].text = text;

			return label;
		}

		/// <summary>Provides input to the panel and it's contents</summary>
		/// <param name="cursor">The cursor to take input from</param>
		public override void Input( Vector2 cursor, bool button )
		{
			if( disabled )
				return;

			base.Input( cursor, button );

			int iteration = 0;
			foreach( Control label in labels )
			{
				label.Input( cursor, button );

				if( label.clicked == true && ItemClicked != null )
					ItemClicked( iteration );

				iteration++;
			}
		}

		public override void Update()
		{
			base.Update();


			for( int i=0; i < labels.Count; i++ )
			{
				Label label = labels[i];

				if( i == selectedIndex )
					label.textColor = new Color( 0.92f, 0.92f, 0.92f, 1.0f );
				else
					label.textColor = new Color( 0.16f, 0.88f, 0.92f, 1.0f );

				label.position = new Vector2( 8, 8 + ( i * labelSize.Y ) + ( i * labelSpacing ) );
				label.disabled = false;
				label.hidden = false;

				label.Update();

				if( label.clicked == true )
				{
					label.color = label.clickColor;
					selectedIndex = i;
				}
			}
		}

		public override void Draw()
		{
			base.Draw();

			// Draw labels
			foreach( Label label in labels )
				label.Draw();

			// Draw title
			if( title != "" )
				Engine.spriteBatch.DrawString( font, title, Position + titleOffset, titleColor, 0, new Vector2( 0, 0 ), Scale, SpriteEffects.None, 0.5f );
		}
	}
}
